package com.cc.controller.web;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.cc.base.PageResponse;
import com.cc.base.ResponseResult;
import com.cc.enums.HttpsCode;
import com.cc.exception.BusinessException;
import com.cc.pojo.dto.LoginDto;
import com.cc.pojo.dto.RegisterDto;
import com.cc.pojo.dto.UserDto;
import com.cc.pojo.dto.UserPageDto;
import com.cc.pojo.vo.UserVo;
import com.cc.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("admin/user/")
@Slf4j
@Tag(name = "管理端用户管理")
public class AdminUserController {
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserService adminUserService;

    /**
     * 注册
     */
    @PostMapping("/register")
    //忽略拦截
    @SaIgnore
    @Operation(summary = "注册")
    public ResponseResult<String> register(@RequestBody RegisterDto registerDto) {
        return adminUserService.register(registerDto);
    }


    /**
     * 添加管理用户
     */
//    @PostMapping()
//    @Operation(summary = "添加管理用户")
//    public ResponseResult<String> addAdminUser(@RequestBody UserDto userDto) {
//        return userService.addAdminUser(userDto);
//    }


    /**
     * 添加用户
     */
    @PostMapping()
    @Operation(summary = "添加用户")
    public ResponseResult<String> addUser(@RequestBody UserDto userDto) {
        return adminUserService.addUser(userDto);
    }

    /**
     * 登录
     *
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    //忽略拦截
    @SaIgnore
    @Operation(summary = "登录")
    public ResponseResult<Map> login(@RequestBody LoginDto loginDto, HttpServletRequest request) {
        return adminUserService.login(loginDto, request);
    }

    /**
     * 是否登录
     *
     * @return
     */
    @GetMapping("isLogin")
    @SaIgnore
    @Operation(summary = "当前会话是否登录")
    public ResponseResult<Boolean> isLogin() {
        return ResponseResult.ok(StpUtil.isLogin());
    }

    /**
     * 注销登录
     *
     * @return
     */
    @GetMapping("logout")
    @Operation(summary = "退出登录成功")
    public ResponseResult<String> logout() {
        StpUtil.logout();
        return ResponseResult.ok("退出登录成功");
    }


    /**
     * 管理员用户信息分页查询 只查询管理员的用户信息
     *
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "用户信息分页查询")
    public ResponseResult<PageResponse<UserVo>> selectAdminUserList(@RequestBody UserPageDto userPageDto) {
        return adminUserService.selectUsers(userPageDto);
    }

    /**
     * 根据id查询用户信息
     *
     * @return
     */
    @GetMapping
    @Operation(summary = "根据id查询用户信息")
    public ResponseResult<UserVo> getById(Integer userId) {
        return adminUserService.getByUserId(userId);
    }

    /**
     * 根据id修改用户信息
     *
     * @return
     */
    @PutMapping
    @Operation(summary = "根据id修改用户信息")
    public ResponseResult<UserVo> updateById(@RequestBody UserDto userDto) {
        return adminUserService.updateUserById(userDto);
    }

    /**
     * 根据id删除用户
     *
     * @return
     */
    @DeleteMapping("/{userId}")
    @Operation(summary = "根据id删除用户")
    public ResponseResult<String> removeById(@PathVariable Integer userId) {
        boolean b = adminUserService.removeById(userId);
        if (!b) {
            throw new BusinessException(HttpsCode.REMOVE_ERROR);
        }
        return ResponseResult.ok("删除成功");
    }

    /**
     * 根据id封禁账户 或者解封账户
     * 永久封禁
     *
     * @return
     */
    @PutMapping("/blockedAndUnseal/{userId}")
    @Operation(summary = "根据id封禁账户")
    public ResponseResult<String> blockedAndUnsealAccount(@PathVariable Integer userId) {
        return adminUserService.blockedAndUnsealAccount(userId);
    }

    /**
     * 踢人下线
     *
     * @return
     */
    @PostMapping("/forceUserLogout")
    @Operation(summary = "踢人下线")
    public ResponseResult<String> forceUserLogout(Integer userId) {
        //检查参数
        if (userId == null) {
            throw new BusinessException(HttpsCode.PARAMS_ERROR);
        }
        StpUtil.logout(userId);
        return ResponseResult.ok("下线成功");
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取当前登录用户信息")
    public ResponseResult<UserVo> getUserInfo() {
        Integer userId = StpUtil.getLoginIdAsInt();
        return adminUserService.getByUserId(userId);
    }
    /**
     * 获取当前登录用户角色列表和权限列表
     * @return
     */
    @GetMapping("/getPermsAndRoles")
    @Operation(summary = "获取当前登录用户角色列表和权限列表")
    public ResponseResult<Map<String,Object>> getPermsAndRoles() {
        Integer userId = StpUtil.getLoginIdAsInt();
        return adminUserService.getPermsAndRoles(userId);
    }
}
